package com.khh.base.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.khh.base.BaseException;

public class ClassLoaderUtil {
	private ClassLoaderUtil() {
		// hide default constructor to prevent instantiation
	}

	public static Class loadClass(String className) {
		try {
			return getClassLoader().loadClass(className);
		} catch (ClassNotFoundException e) {
			throw new BaseException("class not found '" + className + "'", e);
		}
	}

	public static ClassLoader getClassLoader() {
		return ClassLoaderUtil.class.getClassLoader();
	}

	public static InputStream getStream(String resource) {

		InputStream inputStream = ClassLoaderUtil.class.getClassLoader()
				.getResourceAsStream(resource);

		if (inputStream == null) {
			inputStream = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(resource);
		}

		return inputStream;
	}

	public static Properties getProperties(String resource) {
		Properties properties = new Properties();
		try {
			InputStream inStream = getStream(resource);
			properties.load(inStream);
			inStream.close();
		} catch (IOException e) {
			throw new BaseException("couldn't load properties file '"
					+ resource + "'", e);
		}
		return properties;
	}
}
